const { Op } = require('sequelize')
import { Asset } from '../model/asset'
import { CommonUtil } from './../utils/commonUtil'
export class AssetDao {
  queryFundsList(params: any): Promise<Array<Asset>> {
    return Asset.findAll({
      where: {
        date: {
          [Op.substring]: CommonUtil.getDateStr(new Date(params.date))
        },
        creator: params.creator
      },
      order: [['name', 'ASC']],
      limit: params.limit,
      offset: params.offset
    })
  }
  count(params: any): Promise<number> {
    return Asset.count({
      where: {
        date: {
          [Op.substring]: CommonUtil.getDateStr(new Date(params.date))
        },
        creator: params.creator
      }
    })
  }
  /**
   * 根据提供的时间范围获取数据
   * @param {*} params
   * @param {Date} params.startDate
   * @param {Date} params.endDate
   * @param {Date} params.creator
   */
  queryFundsListByDateRange(params: any): Promise<Array<Asset>> {
    const startDate = new Date(params.startDate)
    const endDate = new Date(params.endDate)
    return Asset.findAll({
      where: {
        date: {
          [Op.gte]: startDate,
          [Op.lt]: endDate
        },
        creator: params.creator
      }
    })
  }
  queryAsset(params: any): Promise<Asset> {
    return Asset.findOne({ where: params })
  }
  upsertAsset(params: any): Promise<[Asset, boolean]> {
    return Asset.upsert(params)
  }
  deleteAsset(params: any): Promise<number> {
    return Asset.destroy({ where: params })
  }
}
